#!/bin/sh

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set DBDIFF_HOME if not already set
[ -z "$DBDIFF_HOME" ] && DBDIFF_HOME=`cd "$PRGDIR/.." ; pwd`

if [ -r "$DBDIFF_HOME"/bin/setenv.sh ]; then
  BASEDIR="$DBDIFF_HOME"
  . "$DBDIFF_HOME"/bin/setenv.sh
else
  echo "Cannot find $DBDIFF_HOME/bin/setenv.sh"
  echo "This file is needed to run this program"
  exit 1
fi

# Now switch to home directory of echo
cd $DBDIFF_HOME
# -XX:+HeapDumpOnOutOfMemoryError
JVM_TUNNING="-server -Xms512m -Xprof -Xmx512m -Xmn256m -XX:+UseParallelGC -Dcom.sun.management.jmxremote.port=9001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"

if [ -z "$JAVA_OPTS" ]; then
  JAVA_OPTS="-server -Dfile.encoding=UTF-8 -Djcore.parser=SAX -Djcore.logger=org.apache.log4j -Dlog4j.configuration=log4j.properties -Di.am.here=$DBDIFF_HOME"
else
  JAVA_OPTS="$JAVA_OPTS -server -Dfile.encoding=UTF-8 -Djcore.parser=SAX -Djcore.logger=org.apache.log4j -Dlog4j.configuration=log4j.properties -Di.am.here=$DBDIFF_HOME"
fi

export DBDIFF_PID=$DBDIFF_HOME/dbdiff.pid
export DBDIFF_PRGNAME=$DBDIFF_HOME

check_process_exits()
{
  if [ -f "$DBDIFF_PID" ]; then
    pid=`cat $DBDIFF_PID`
    [ -z $pid ] &&  pid="-1"
    kill -0 $pid > /dev/null 2>&1
    find_pids=`ps aux | grep $DBDIFF_PRGNAME | grep -v 'grep' | awk '{print $2}'`
    for find_pid in $find_pids
    do  
      if [ $find_pid = $pid ]; then
        echo "Process exists, can't start new dbdiff process"
        exit 1
      fi  
    done
  fi
}

check_int()
{
  echo $1 | grep "[^0-9]" > /dev/null && value=0 || value=1
  return $value
}

if [ "$1" = "start" -o "$1" = "proceed" ] ; then
  check_process_exits
  cmd=$1
  shift
  while getopts ":d:s:c:" OPT;do
    case $OPT in
      d)
        check_int $OPTARG
        var1=$?
	if [ $var1 = 1 ] ; then
	  D="D$OPTARG"
	else
	  echo "Invalid option : "$OPTARG", missing dump thread count."
          exit 1
	fi
        ;;
      s)
        check_int $OPTARG
        var2=$?
	if [ $var2 = 1 ] ; then
	  D="D$OPTARG"
	else
	  echo "Invalid option : "$OPTARG", missing sort thread count."
          exit 1
	fi
        ;;
      c)
        check_int $OPTARG
        var3=$?
	if [ $var3 = 1 ] ; then
	  D="D$OPTARG"
	else
	  echo "Invalid option : "$OPTARG", missing compare thread count."
          exit 1
	fi
        ;;
      *)
        echo "Invalid option : "$OPTARG
        echo "Usage: dbdiff.sh <start|proceed> [-d <dumpcount>] [-s <sortcount>] [-c <comparecount>]"
        exit 1
        ;;
    esac
  done  
  $RUNJAVA -classpath $CLASSPATH $JAVA_OPTS $JVM_TUNNING\
    com.yoyosys.dbtoolkit.dbdiff.ScheduleManager "$cmd" "$D$S$C" &
  echo $! > $DBDIFF_PID
  echo "Start success."
elif [ "$1" = "stop" ] ; then
  if [ -f "$DBDIFF_PID" ]; then
    kill -9 `cat $DBDIFF_PID` > /dev/null 2>&1 &
    rm $DBDIFF_PID
    echo "Stop success."
  else
    echo "Stop failed: $DBDIFF_PID not exist"
  fi
elif [ "$1" = "status" ] ; then
  if [ -f "$DBDIFF_PID" ]; then
    pid=`cat $DBDIFF_PID`
    [ -z $pid ] &&  pid="-1"
    kill -0 $pid > /dev/null 2>&1
    find_pids=`ps aux | grep $DBDIFF_PRGNAME | grep -v 'grep' | awk '{print $2}'`
    for find_pid in $find_pids
    do
      if [ $find_pid = $pid ]; then
        exit 0
      fi  
    done
    exit 1
  else
    exit 1
  fi
else
  echo "Usage: dbdiff.sh <start|proceed> [-d <dumpcount>] [-s <sortcount>] [-c <comparecount>]"
  echo "params:"
  echo "  -start              To compare all of the data based on the current profile."
  echo "  -proceed            Continue to the unfinished job based on the current profile."
  echo "options:"
  echo "  -d <dumpcount>      Dump thread count."
  echo "  -s <sortcount>      Sort thread count."
  echo "  -c <comparecount>   Compare thread count."
  exit 1
fi
